/* eslint-disable */
import * as THREE from 'three';
/**
 * 指示点
 * @param  options.texture 标点材质纹理
 * @param  options.scaleValue 缩放原始值
 * @param  options.isadd Boolean 判断缩放值的增减
 * @param  options.scene 场景
 * @param  options.positionAddUserData 各点的位置及对应分配的userData需携带的信息
 * @param  options.group 存储点数据
 * @example
 */

class IndicationPoint {
  constructor(option) {
    this.texture = option.texture || '';
    this.scaleValue = option.scaleValue || 0.2;
    this.isadd = option.isadd || false;
    this.scene = option.scene;
    this.positionAddUserData = option.positionAddUserData || [];
    this.group = new THREE.Group()
    this.CreatIndicationPoint();
  }
  CreatIndicationPoint() {
    var spriteMaterial = new THREE.SpriteMaterial({
      map: this.texture,
      color: 0xffffff,
    });

    //传入的多点的话生成多个模型
    for (let i = 0; i < this.positionAddUserData.length; i += 1) {
      let point = new THREE.Sprite(spriteMaterial);

      let position = this.positionAddUserData[i].position
      //设置模型位置
      point.position.set(position[0], position[1], position[2]);
      let userData = this.positionAddUserData[i].userData
      Object.keys(userData).forEach(key => {
        point.userData[key] = userData[key]
      })
      this.group.add(point)
    }
    this.scene.add(this.group);

    //创建render，每一帧进行模型改变
    this.thing = setInterval(() => {
      this.scaleValue += this.isadd ? 0.01 : -0.01;
      //循环所有创建的模型，进行改变
      let points = this.group.children
      for (let i = 0; i < points.length; i++) {
        points[i].scale.set(this.scaleValue, this.scaleValue, this.scaleValue);
      }
      this.isadd =
        this.scaleValue < 0.1 || this.scaleValue > 0.2 ? !this.isadd : this.isadd;
    }, 100);
  }
  delete() {
    //删除scene中对应的指示点
    this.scene.remove(this.group);
    if (this.thing) {
      //清除render事件
      clearInterval(this.thing);
    }
  }
}
export default IndicationPoint;